﻿using DataService;
using HMIControl;
using System.Threading.Tasks;
using System.Windows;

namespace CoreTest
{
    /// <summary>
    /// Interaction logic for StartDevice.xaml
    /// </summary>
    public partial class StartDevice : Window
    {
        const int COLS = 4;
        const int ROWHEIGHT = 30;
        public static string Dev_RunStopString = "_Run";


        public StartDevice(Motor ui)
        {
            InitializeComponent();

            string dev = ui.DeviceName;
            InitStartDevice(dev);
            InitSetSpeedDevice(ui);
            Point p = ui.PointToScreen(new Point(0, 0));
            InitUIPosion(p);
        }

        private void InitSFStartDevice(SFMotor ui)
        {
            devicename.Text += ":" + ui.DeviceName;
            btn_speed.IsEnabled = false;
            btn_yl.IsEnabled = false;
            sp_speed.Visibility = Visibility.Collapsed;
            sp_yl.Visibility = Visibility.Collapsed;

            btnStart.Click += new RoutedEventHandler((s, e) =>
            {
                var tag = App.Server[ui.StartCMDTag];
                WriteAndClose(tag, true);
                WriteAndClose(tag, false);
            });
            btnStop.Click += new RoutedEventHandler((s, e) =>
            {
                var tag = App.Server[ui.StopCMDTag];
                WriteAndClose(tag, true);
                WriteAndClose(tag, false);
            });
        }

        public StartDevice(SFMotor ui)
        {
            InitializeComponent();

            string dev = ui.DeviceName;
            InitSFStartDevice(ui);
            InitSetSpeedDevice(ui);
            Point p = ui.PointToScreen(new Point(0, 0));
            InitUIPosion(p);
        }

        

        void InitSetSpeedDevice(SpeedBase ui)
        {
            sp_speed.Visibility = Visibility.Visible;
            btn_speed.IsEnabled = true;
            ITag tag_Speed = App.Server[ui.SetSpeedTag];

            if (tag_Speed != null)
                txt_speed.Text = tag_Speed.Read().Int32.ToString();

            btn_speed.Click += new RoutedEventHandler((s, e) =>
          {
              WriteAndClose(tag_Speed, txt_speed.Text);

          });
        }

        private void WriteAndClose(ITag tag, object val)
        {
            Task t1 = new Task(() =>
            {
                if (tag != null)
                    tag.Write(val);
            });

            //t1.ContinueWith(t =>
            //{
            //    this.Dispatcher.Invoke(() => { this.Close(); });
            //});

            t1.Start();
            this.Close();
        }


        public StartDevice(Elevator ui)
        {
            InitializeComponent();

            string dev = ui.DeviceName;
            InitStartDevice(dev);
            InitSetSpeedDevice(ui);
            Point p = ui.PointToScreen(new Point(0, 0));
            InitUIPosion(p);
        }

        private void InitStartDevice(string dev)
        {
            devicename.Text += ":" + dev;
            btn_speed.IsEnabled = false;
            btn_yl.IsEnabled = false;
            sp_speed.Visibility = Visibility.Collapsed;
            sp_yl.Visibility = Visibility.Collapsed;

            btnStart.Click += new RoutedEventHandler((s, e) =>
            {
                var tag = App.Server[dev + Dev_RunStopString];
                WriteAndClose(tag, true);
            });
            btnStop.Click += new RoutedEventHandler((s, e) =>
            {
                var tag = App.Server[dev + Dev_RunStopString];
                WriteAndClose(tag, false);
            });

        }

        private void InitUIPosion(Point p)
        {
            this.SizeToContent = SizeToContent.WidthAndHeight;
            this.Left = p.X;
            this.Top = p.Y;
            if (this.Left + this.Width >= SystemParameters.WorkArea.Width)
                this.Left = SystemParameters.WorkArea.Width - this.Width;
            if (this.Top + this.Height >= SystemParameters.WorkArea.Height)
                this.Top = SystemParameters.WorkArea.Height - this.Height;
        }


        public StartDevice(string dev, Point p)
        {
            InitializeComponent();
            InitStartDevice(dev);
            InitUIPosion(p);
        }

        private void btnExit_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
    }
}
